//
//  HTAPI.m
//  hunter
//
//  Created by MEOS on 15/7/13.
//  Copyright (c) 2015年 HT. All rights reserved.
//

#import "HTAPI.h"
#import "UIDevice+Hardware.h"
//#import "Reachability.h"
//#import "HTAPIString.h"
#import "SVProgressHUD.h"

static NSString *kAPIURLString = @"http://14.29.116.14:28098/ios/";



/*
 
 测试环境地址 h ttp://14.29.116.14:28098/
 
 lee    123123  测试帐号
 
  ios    123123  测试帐号
 */

@interface HTAPI ()

@end

@implementation HTAPI



#pragma mark -
#pragma mark - basic method

+ (instancetype)shareInstance{
    
    static HTAPI *_sharedAPI = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedAPI = [[HTAPI alloc] initWithBaseURL:[NSURL URLWithString:kAPIURLString]];
        [_sharedAPI.requestSerializer setValue:[_sharedAPI customHeader] forHTTPHeaderField:@"User-Agent"];
        _sharedAPI.requestSerializer.timeoutInterval = 15;
        [_sharedAPI.reachabilityManager startMonitoring];
//        [_sharedAPI.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//            ////HTLog(@"AFNetworkReachabilityStatus: %ld",(long)status);
//            if (status ==AFNetworkReachabilityStatusNotReachable) {
//                [SVProgressHUD showErrorWithStatus:@"网络无法连接\n请检查网络设置"];
//
//            }
//            
//        }];
    });
    return _sharedAPI;
}



- (NSString*)customHeader
{
    NSString *systemName = [[[UIDevice currentDevice] systemName]
                            stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
    NSString *model = [[UIDevice currentDevice] hardwareDescription];
    NSString *appMessage = [NSString stringWithFormat:@"%@ %@",
                            [[NSBundle mainBundle] infoDictionary][(NSString *)kCFBundleNameKey],
                            [[NSBundle mainBundle] infoDictionary][(NSString *)kCFBundleVersionKey]];
    NSString *userAgent = [NSString stringWithFormat:@"%@ %@|%@|%@",systemName,systemVersion,model,appMessage];
    return userAgent;
}

- (AFHTTPRequestOperation *)performGetRequestWithURL:(NSString *)_url parameters:(NSDictionary *)_parameters success:(APIClientSuccess)_success failure:(APIClientFailure)_failure{
    NSMutableDictionary *defaultParameters = [NSMutableDictionary dictionaryWithDictionary:_parameters];
    
//    [self.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        //HTLog(@"AFNetworkReachabilityStatus: %ld",(long)status);
//        if (status ==AFNetworkReachabilityStatusNotReachable) {
//            [SVProgressHUD showErrorWithStatus:@"网络无法连接\n请检查网络设置"];
//            return ;
//        }}];
    

    NSLog(@"----parmas----%@",_parameters);

    AFHTTPRequestOperation * requestOperation = [self GET:_url parameters:defaultParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"--operation---%@",operation);

        if (_success) {
            _success(operation,responseObject);
            
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"--operation---%@",operation);
        if (_failure) {
            _failure(operation, operation.responseObject);
        }
        
        
//        [SVProgressHUD showErrorWithStatus:@"网络请求超时,请重试"];


    }];
    
    return requestOperation;
}

- (AFHTTPRequestOperation *)performPostRequestWithURL:(NSString *)_url
                                           parameters:(NSDictionary *)_parameters
                                              success:(APIClientSuccess)_success
                                              failure:(APIClientFailure)_failure{
    NSMutableDictionary *defaultParameters = [NSMutableDictionary dictionaryWithDictionary:_parameters];

    

    [self.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        //HTLog(@"AFNetworkReachabilityStatus: %ld",(long)status);
        if (status ==AFNetworkReachabilityStatusNotReachable) {
            [SVProgressHUD showErrorWithStatus:@"网络无法连接\n请检查网络设置"];
            return ;
        }}];
        

    AFHTTPRequestOperation * requestOperation = [self POST:_url parameters:defaultParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
        if (_success) {
            _success(operation,responseObject);
            
        }
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"--operation---%@",operation);
        if (_failure) {
            _failure(operation, operation.responseObject);
        }
        
        
//        [SVProgressHUD showErrorWithStatus:@"网络请求超时,请重试"];

        
    }];
    return requestOperation;
}

- (AFHTTPRequestOperation *)performPostRequestWithURL:(NSString *)_url
                                           parameters:(NSDictionary *)_parameters
                            constructingBodyWithBlock:(void (^)(id<AFMultipartFormData>))block
                                              success:(APIClientSuccess)_success
                                              failure:(APIClientFailure)_failure
{
    NSMutableDictionary *defaultParameters = [NSMutableDictionary dictionaryWithDictionary:_parameters];

    
    AFHTTPRequestOperation * requestOperation = [self POST:_url
                                                parameters:defaultParameters
                                 constructingBodyWithBlock:block
                                                   success:^(AFHTTPRequestOperation *operation, id responseObject) {

                                                   }
                                                   failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                                                   }];
    return requestOperation;
}

//
////* 登录
//-(void)loginWithParameters:(NSDictionary*)_parameters success:(APIClientSuccess)_success failure:(APIClientFailure)_failure{
//    NSMutableDictionary *defaultParameters = [NSMutableDictionary dictionaryWithDictionary:_parameters];
//    
////    [defaultParameters setObject:@"ios" forKey:@"username"];
////    [defaultParameters setObject:@"123123" forKey:@"password"];
//    
//    [self performPostRequestWithURL:API_Login parameters:defaultParameters success:_success failure:_failure];
//    
//}
//
//
//


//* 2.15  - 2.22 定义修改
- (void)Action:(NSString*)HTAPIString WithParameters:(NSDictionary*)_parameters success:(APIClientSuccess)_success failure:(APIClientFailure)_failure{
    
        NSMutableDictionary *defaultParameters = [NSMutableDictionary dictionaryWithDictionary:_parameters];
        [self performPostRequestWithURL:HTAPIString parameters:defaultParameters success:_success failure:_failure];
    
}

//YJB 2016.5.19 运价宝 处理get请求
-(void)YJBActionWithGetRequset:(NSString*)YJBAPIString WithParameters:(NSDictionary*)_parameters success:(APIClientSuccess)_success failure:(APIClientFailure)_failure{
    
    NSMutableDictionary *defaultParameters = [NSMutableDictionary dictionaryWithDictionary:_parameters];
    [self performGetRequestWithURL:YJBAPIString parameters:defaultParameters success:_success failure:_failure];
    
}

////* 2.2获取用户信息
//- (void)getUserInfoWithParameters:(NSDictionary*)_parameters success:(APIClientSuccess)_success failure:(APIClientFailure)_failure{
//    NSMutableDictionary *defaultParameters = [NSMutableDictionary dictionaryWithDictionary:_parameters];
//    [self performPostRequestWithURL:API_UserInfo parameters:defaultParameters success:_success failure:_failure];
//}




@end
